package com.mifengs.mall.ui.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.content.ContextCompat;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.Result;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.mall.R;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.mall.util.ToIntentUtil;
import com.mifengs.mall.widget.CustomViewFinderView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.dm7.barcodescanner.core.IViewFinder;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class ScanActivity extends BaseActivity  implements ZXingScannerView.ResultHandler {

    @BindView(R.id.content_frame)
    FrameLayout mContentFrame;
    @BindView(R.id.iv_light)
    ImageView mIvLight;
    @BindView(R.id.tv_light)
    TextView mTvLight;
    private ZXingScannerView mScannerView;
    private boolean isLight;
    private CustomViewFinderView mCustomViewFinderView;
    private static final String FLASH_STATE = "FLASH_STATE";
    public static final int TYPE_SCAN_QUHUO = 1;
    private int mScanType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        ButterKnife.bind(this);
        initNavigationBar(true, getString(R.string.scan_title));
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this,R.color.nav_color));
        StatusBarUtils.StatusBarLightMode(this);

        Intent intent = getIntent();
        mScanType = intent.getIntExtra("scanType",0);

        mScannerView = new ZXingScannerView(this) {
            @Override
            protected IViewFinder createViewFinderView(Context context) {
                mCustomViewFinderView = new CustomViewFinderView(context);
                return mCustomViewFinderView;
            }
        };
        mContentFrame.addView(mScannerView);
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this);
        mScannerView.startCamera();
        mScannerView.setFlash(isLight);
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putBoolean(FLASH_STATE, isLight);
    }

    @Override
    public void handleResult(Result rawResult) {
        String content = rawResult.getText();
        if(mScanType==1){
            Intent intent = new Intent();
            intent.putExtra("content",content);
            setResult(RESULT_OK,intent);
            finish();
        }else{
            if(content.contains("://")){
                String[] contentSplits = content.split("://");
                if(contentSplits.length>=1){
                    String prefix = contentSplits[0];
                    if(prefix.startsWith("http")){
                        ToIntentUtil.toCommonIntent(content,this);
                        finish();
                    }
                }
                setResult(RESULT_OK);
            }else{
                mScannerView.resumeCameraPreview(this);
                showLongToast(content);
            }
        }
    }



    @OnClick(R.id.ll_flash)
    public void onViewClicked() {
        isLight = !isLight;
        mScannerView.setFlash(isLight);
        if (isLight) {
            mIvLight.setImageResource(R.drawable.flash_light_up);
            mTvLight.setText(getString(R.string.close_light));
        } else {
            mIvLight.setImageResource(R.drawable.flash_light_down);
            mTvLight.setText(getString(R.string.open_light));
        }
    }
}
